package com.ewing.graphqltest.graphql;

import com.ewing.graphqltest.model.UserEntity;
import com.ewing.graphqltest.model.VideoEntity;
import com.ewing.graphqltest.service.UserService;
import com.ewing.graphqltest.service.VideoService;
import graphql.schema.DataFetcher;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@AllArgsConstructor
public class UserGrapQLDataFetchers {
    private UserService userService;

    private VideoService videoService;

    public DataFetcher findUserById(){
        return dataFetchingEnvironment -> {
            Integer id = Integer.parseInt(dataFetchingEnvironment.getArgument("id"));
            UserEntity userEntity = userService.findById(id);
            return userEntity;
        };
    }

    public DataFetcher findVideoByUser(){
        return dataFetchingEnvironment -> {
            UserEntity user = (UserEntity) dataFetchingEnvironment.getSource();
            List<VideoEntity> videoEntityList = videoService.findByUserId(user.getId());
            return videoEntityList;
        };
    }
}
